<template>
  <div class="smart-matching-center">
    <!-- 匹配操作区域 -->
    <div class="match-actions">
      <el-button 
        type="primary" 
        :icon="Lightning" 
        @click="$emit('quick-match', selectedTransactions, selectedOrders)"
        :disabled="!canQuickMatch"
        size="large"
        class="action-btn"
      >
        快速匹配 (Ctrl+Q)
      </el-button>
      
      <el-button 
        :icon="Search" 
        @click="showMatchDetails"
        :disabled="!hasSelections"
        class="action-btn"
      >
        查看详情
      </el-button>
      
      <el-button 
        :icon="Close" 
        @click="clearSelection"
        :disabled="!hasSelections"
        class="action-btn"
      >
        清除选择
      </el-button>
    </div>

    <!-- 匹配状态指示器 -->
    <div class="match-status" v-if="matchingStatus !== 'idle'">
      <el-progress 
        v-if="matchingStatus === 'matching'"
        type="circle" 
        :percentage="matchProgress"
        :width="100"
        status="success"
        class="matching-progress"
      >
        <div class="progress-content">
          <div class="progress-text">智能匹配中</div>
          <div class="progress-percent">{{ matchProgress }}%</div>
        </div>
      </el-progress>
      <el-result 
        v-else-if="matchingStatus === 'success'"
        icon="success" 
        title="匹配完成" 
        :sub-title="`找到 ${matchResults.length} 个匹配结果`"
        class="match-result"
      />
      <el-result 
        v-else-if="matchingStatus === 'error'"
        icon="error" 
        title="匹配失败" 
        sub-title="请稍后重试"
        class="match-result"
      />
    </div>

    <!-- 匹配结果预览 -->
    <div class="match-preview" v-if="hasPreviewData">
      <h3>
        <el-icon><View /></el-icon>
        匹配预览
      </h3>
      <div class="preview-content">
        <div class="preview-item">
          <span class="label">选中流水:</span>
          <span class="value">{{ selectedTransactions.length }} 条</span>
        </div>
        <div class="preview-item">
          <span class="label">选中订单:</span>
          <span class="value">{{ selectedOrders.length }} 条</span>
        </div>
        <div class="preview-item">
          <span class="label">预计匹配:</span>
          <span class="value">{{ Math.min(selectedTransactions.length, selectedOrders.length) }} 组</span>
        </div>
        <div class="preview-item">
          <span class="label">置信度范围:</span>
          <span class="value">{{ confidenceRange }}</span>
        </div>
      </div>
    </div>

    <!-- 算法选择 -->
    <div class="algorithm-selector">
      <span class="selector-label">
        <el-icon><Setting /></el-icon>
        匹配算法:
      </span>
      <el-select 
        v-model="currentAlgorithm" 
        @change="$emit('algorithm-change', $event)"
        size="small"
        class="algorithm-select"
      >
        <el-option label="综合算法" value="comprehensive">
          <span class="algorithm-option">
            <el-icon><DataAnalysis /></el-icon>
            综合算法
            <el-tooltip content="综合考虑金额、名称、时间等多个因素进行匹配，适用于大多数场景" placement="top">
              <el-icon class="info-icon"><QuestionFilled /></el-icon>
            </el-tooltip>
          </span>
        </el-option>
        <el-option label="精确匹配" value="exact">
          <span class="algorithm-option">
            <el-icon><Check /></el-icon>
            精确匹配
            <el-tooltip content="只匹配金额完全相同且名称高度相似的记录，准确率最高但匹配数量较少" placement="top">
              <el-icon class="info-icon"><QuestionFilled /></el-icon>
            </el-tooltip>
          </span>
        </el-option>
        <el-option label="金额优先" value="amount_first">
          <span class="algorithm-option">
            <el-icon><Coin /></el-icon>
            金额优先
            <el-tooltip content="优先按金额相似度进行匹配，适合处理金额有微小差异的情况" placement="top">
              <el-icon class="info-icon"><QuestionFilled /></el-icon>
            </el-tooltip>
          </span>
        </el-option>
        <el-option label="时间窗口" value="time_window">
          <span class="algorithm-option">
            <el-icon><Timer /></el-icon>
            时间窗口
            <el-tooltip content="在指定时间范围内进行匹配，适合处理时间相近的交易记录" placement="top">
              <el-icon class="info-icon"><QuestionFilled /></el-icon>
            </el-tooltip>
          </span>
        </el-option>
      </el-select>
    </div>

    <!-- 智能建议 -->
    <div class="smart-suggestions" v-if="smartSuggestions.length > 0">
      <h4>
        <el-icon><MagicStick /></el-icon>
        智能建议
      </h4>
      <div class="suggestions-list">
        <div 
          v-for="suggestion in smartSuggestions" 
          :key="suggestion.id"
          class="suggestion-item"
          :class="`suggestion-${suggestion.priority}`"
        >
          <div class="suggestion-content">
            <el-icon class="suggestion-icon"><Lightning /></el-icon>
            <span class="suggestion-text">{{ suggestion.description }}</span>
            <el-tag size="small" :type="getPriorityType(suggestion.priority)" class="confidence-tag">
              {{ suggestion.confidence }}% 置信度
            </el-tag>
          </div>
          <div class="suggestion-actions">
            <el-button 
              type="primary" 
              link 
              @click="applySuggestion(suggestion)"
              class="action-link"
            >
              应用
            </el-button>
            <el-button 
              type="danger" 
              link 
              @click="dismissSuggestion(suggestion)"
              class="action-link"
            >
              忽略
            </el-button>
          </div>
        </div>
      </div>
    </div>

    <!-- 空状态 -->
    <div class="empty-state" v-else-if="!hasPreviewData && matchingStatus === 'idle'">
      <el-empty description="请选择银行流水和销售订单进行匹配">
        <el-button type="primary" @click="$emit('show-instructions')">
          查看操作指南
        </el-button>
      </el-empty>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import { 
  Lightning, 
  Search, 
  Close, 
  View, 
  Setting, 
  DataAnalysis, 
  Check, 
  Coin, 
  Timer,
  MagicStick,
  QuestionFilled
} from '@element-plus/icons-vue'

interface Props {
  selectedTransactions: any[]
  selectedOrders: any[]
  matchResults: any[]
  matchingStatus: 'idle' | 'matching' | 'success' | 'error'
  matchProgress?: number
  smartSuggestions?: any[]
}

const props = withDefaults(defineProps<Props>(), {
  selectedTransactions: () => [],
  selectedOrders: () => [],
  matchResults: () => [],
  matchProgress: 0,
  smartSuggestions: () => []
})

// 修复emit定义
const emit = defineEmits<{
  (e: 'quick-match', transactions: any[], orders: any[]): void;
  (e: 'confirm-match', matchId: string): void;
  (e: 'reject-match', matchId: string): void;
  (e: 'show-details'): void;
  (e: 'algorithm-change', algorithm: string): void;
  (e: 'show-instructions'): void;
  (e: 'clear-selection'): void;
  (e: 'dismiss-suggestion', suggestion: any): void;
}>()

const currentAlgorithm = ref('comprehensive')

const canQuickMatch = computed(() => 
  props.selectedTransactions.length > 0 && props.selectedOrders.length > 0
)

const hasSelections = computed(() => 
  props.selectedTransactions.length > 0 || props.selectedOrders.length > 0
)

const hasPreviewData = computed(() => 
  props.selectedTransactions.length > 0 || props.selectedOrders.length > 0
)

const confidenceRange = computed(() => {
  // 模拟置信度范围计算
  if (!hasPreviewData.value) return '0-0%'
  const min = 70
  const max = 95
  return `${min}-${max}%`
})

const showMatchDetails = () => {
  emit('show-details')
}

const clearSelection = () => {
  // 清除选择逻辑
  emit('clear-selection')
}

const applySuggestion = (suggestion: any) => {
  suggestion.action()
}

const dismissSuggestion = (suggestion: any) => {
  // 忽略建议逻辑
  emit('dismiss-suggestion', suggestion)
}

const getPriorityType = (priority: string) => {
  switch (priority) {
    case 'high': return 'success'
    case 'medium': return 'warning'
    case 'low': return 'info'
    default: return 'info'
  }
}
</script>

<style scoped lang="scss">
.smart-matching-center {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 24px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  
  .match-actions {
    display: flex;
    gap: 16px;
    margin-bottom: 24px;
    flex-wrap: wrap;
    
    .action-btn {
      flex: 1;
      min-width: 130px;
      height: 48px;
      font-size: 15px;
      border-radius: 6px;
      
      &:first-child {
        flex: 2;
      }
    }
  }
  
  .match-status {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 24px 0;
    min-height: 150px;
    
    .matching-progress {
      .progress-content {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        text-align: center;
        
        .progress-text {
          font-size: 14px;
          color: #606266;
          margin-bottom: 4px;
        }
        
        .progress-percent {
          font-size: 20px;
          font-weight: bold;
          color: #409eff;
        }
      }
    }
    
    .match-result {
      :deep(.el-result__title) {
        font-size: 20px;
        font-weight: 600;
      }
      
      :deep(.el-result__subtitle) {
        font-size: 15px;
      }
    }
  }
  
  .match-preview {
    background: linear-gradient(135deg, #f0f9eb 0%, #e6f4ea 100%);
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 24px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
    
    h3 {
      margin: 0 0 16px 0;
      font-size: 18px;
      font-weight: 600;
      color: #303133;
      display: flex;
      align-items: center;
      gap: 8px;
    }
    
    .preview-content {
      .preview-item {
        display: flex;
        justify-content: space-between;
        margin-bottom: 12px;
        padding: 8px 0;
        border-bottom: 1px dashed #dcdfe6;
        
        &:last-child {
          border-bottom: none;
          margin-bottom: 0;
        }
        
        .label {
          color: #606266;
          font-weight: 500;
        }
        
        .value {
          font-weight: 600;
          color: #303133;
        }
      }
    }
  }
  
  .algorithm-selector {
    display: flex;
    align-items: center;
    gap: 16px;
    margin-bottom: 24px;
    padding: 16px;
    background: linear-gradient(135deg, #e6f7ff 0%, #d9f0ff 100%);
    border-radius: 8px;
    
    .selector-label {
      font-size: 15px;
      font-weight: 500;
      color: #303133;
      display: flex;
      align-items: center;
      gap: 6px;
    }
    
    .algorithm-select {
      flex: 1;
      
      :deep(.el-select__wrapper) {
        background: rgba(255, 255, 255, 0.8);
        border: 1px solid #d9f0ff;
        border-radius: 6px;
      }
    }
    
    .algorithm-option {
      display: flex;
      align-items: center;
      gap: 8px;
      
      .el-icon {
        font-size: 16px;
      }
      
      .info-icon {
        font-size: 14px;
        color: #909399;
        margin-left: 4px;
      }
    }
  }
  
  .smart-suggestions {
    h4 {
      margin: 0 0 16px 0;
      font-size: 18px;
      font-weight: 600;
      color: #303133;
      display: flex;
      align-items: center;
      gap: 8px;
    }
    
    .suggestions-list {
      .suggestion-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 16px;
        margin-bottom: 12px;
        border-radius: 8px;
        background: linear-gradient(135deg, #fffbe6 0%, #fff7e6 100%);
        border: 1px solid #ffe58f;
        
        &.suggestion-high {
          background: linear-gradient(135deg, #f6ffed 0%, #f0ffe6 100%);
          border-color: #b7eb8f;
        }
        
        &.suggestion-medium {
          background: linear-gradient(135deg, #fffbe6 0%, #fff7e6 100%);
          border-color: #ffe58f;
        }
        
        &.suggestion-low {
          background: linear-gradient(135deg, #e6f7ff 0%, #d9f0ff 100%);
          border-color: #91d5ff;
        }
        
        .suggestion-content {
          display: flex;
          align-items: center;
          gap: 12px;
          
          .suggestion-icon {
            font-size: 20px;
            color: #fa8c16;
          }
          
          .suggestion-text {
            font-size: 15px;
            font-weight: 500;
            color: #303133;
          }
          
          .confidence-tag {
            margin-left: 8px;
          }
        }
        
        .suggestion-actions {
          display: flex;
          gap: 8px;
        }
      }
    }
  }
  
  .empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
}
</style>